package com.dubbo.server.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dubbo.server.mapper.LogMapper;
import com.dubbo.server.mapper.PublishInfoMapper;
import com.dubbo.server.mapper.UserInfoMapper;

import com.dubbo.server.pojo.Log;
import com.dubbo.server.pojo.PublishInfo;
import com.dubbo.server.pojo.UserInfo;
import com.dubbo.server.vo.*;
import com.tanhua.common.pojo.Publish;
import com.tanhua.common.pojo.Video;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class MessagesPagesService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private PublishInfoMapper publishInfoMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private LogMapper logMapper;

    /**
     * 翻页查询用户动态
     *
     * @param
     * @return
     */
    public MessagePageResult queryMessages(MessageParam messageParam) {

        //取出分页数据
        //如果没有就赋值为-1
        Integer page = messageParam.getPage();
        Integer pageSize = messageParam.getPagesize();
        //补全返回对象messagePageResult
        MessagePageResult messagePageResult = new MessagePageResult();
        messagePageResult.setPage(page);
        messagePageResult.setPagesize(pageSize);
        Query query = new Query();
        query.skip((page - 1) * pageSize);
        query.limit(pageSize);
        query.with(Sort.by(Sort.Order.desc("created")));
        List<Publish> publishes = mongoTemplate.find(query, Publish.class);

        List<MessageResult> results = new ArrayList<>();
        for (Publish publish : publishes) {
            PublishInfo publishInfo = publishInfoMapper.selectById(publish.getUserId());
            int i = (page - 1) * 10;
            i++;
            MessageResult messageResult = new MessageResult();
            messageResult.setId(String.valueOf(publish.getId()));//测试
            messageResult.setPid(String.valueOf(publish.getPid()));//测试
            messageResult.setCreateDate(publish.getCreated());
            messageResult.setText(publish.getText());
            messageResult.setReportCount(publishInfo.getReportCount());//举报数
            messageResult.setLikeCount(publishInfo.getLoveCount());//喜欢数
            messageResult.setForwardingCount(publishInfo.getForwardingCount());//转发数
            messageResult.setCommentCount(publishInfo.getCommentCount());//评论数
            results.add(messageResult);
        }
        messagePageResult.setItems(results);
        return messagePageResult;

    }


    /**
     * 翻页查询视频
     *
     * @param
     * @return
     */
    public MessagePageResult videosQueryMessages(MessageParam messageParam) {

        //取出分页数据
        //如果没有就赋值为-1
        Integer page = messageParam.getPage();
        Integer pageSize = messageParam.getPagesize();
        //补全返回对象messagePageResult
        MessagePageResult messagePageResult = new MessagePageResult();
        messagePageResult.setPage(page);
        messagePageResult.setPagesize(pageSize);
        Query query = new Query();
        query.skip((page - 1) * pageSize);
        query.limit(pageSize);
        query.with(Sort.by(Sort.Order.desc("created")));
        List<Video> videos = mongoTemplate.find(query, Video.class);

        List<MessageResult> results = new ArrayList<>();
        int i = 0;
        for (Video video : videos) {
            PublishInfo publishInfo = publishInfoMapper.selectById(video.getUserId());
            i = (page - 1) * 10;
            i++;
            MessageResult messageResult = new MessageResult();
            messageResult.setId(String.valueOf(i));
            messageResult.setCreateDate(video.getCreated());
            messageResult.setVideoUrl(video.getVideoUrl());
            messageResult.setPicUrl(video.getPicUrl());
            if (publishInfo == null) {
                messageResult.setReportCount(12);//举报数
                messageResult.setLikeCount(37);//喜欢数
                messageResult.setForwardingCount(10);//转发数
                messageResult.setCommentCount(102);//评论数
            } else {
                messageResult.setReportCount(publishInfo.getReportCount());//举报数
                messageResult.setLikeCount(publishInfo.getLoveCount());//喜欢数
                messageResult.setForwardingCount(publishInfo.getForwardingCount());//转发数
                messageResult.setCommentCount(publishInfo.getCommentCount());//评论数
            }
            results.add(messageResult);
        }
        messagePageResult.setItems(results);
        return messagePageResult;

    }


    /**
     * 翻页查询用户登录记录
     *
     * @param
     * @return
     */
    public MessagePageResult logQueryMessages(MessageParam messageParam) {

        //取出分页数据
        //如果没有就赋值为-1
        Integer page = messageParam.getPage();
        Integer pageSize = messageParam.getPagesize();
        //补全返回对象messagePageResult
        MessagePageResult messagePageResult = new MessagePageResult();
        messagePageResult.setPage(page);
        messagePageResult.setPagesize(pageSize);
//        Query query = new Query();
//        query.skip((page - 1) * pageSize);
//        query.limit(pageSize);
//        query.with(Sort.by(Sort.Order.desc("created")));
        IPage<Log> userPage = new Page<>(page, pageSize);
        QueryWrapper<Log> query = new QueryWrapper<>();
        query.orderByDesc("created");
        query.eq("user_id", messageParam.getUid());
        IPage<Log> logIPage = logMapper.selectPage(userPage, query);
        List<MessageResult> results = new ArrayList<>();
        List<Log> records = logIPage.getRecords();
        int i = 0;
        for (Log record : records) {
            i = (page - 1) * 10;
            i++;
            MessageResult messageResult = new MessageResult();
            messageResult.setId(String.valueOf(i));
            messageResult.setLogTime(record.getCreated().getTime());
            messageResult.setPlace(record.getPlace());
            messageResult.setEquipment(record.getEquipment());
            results.add(messageResult);
        }
        messagePageResult.setItems(results);
        return messagePageResult;

    }


}
